home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / includes / database.pgsql.inc < prev    next >
Encoding:
Text File  |  2005-04-08  |  6.1 KB  |  238 lines

  1. <?php
  2. // $Id: database.pgsql.inc,v 1.6.2.1 2005/04/08 11:24:25 dries Exp $
  3.  
  4. /**
  5.  * @file
  6.  * Database interface code for PostgreSQL database servers.
  7.  */
  8.  
  9. /**
  10.  * @ingroup database
  11.  * @{
  12.  */
  13.  
  14. /**
  15.  * Initialize a database connection.
  16.  *
  17.  * Note that you can change the pg_connect() call to pg_pconnect() if you
  18.  * want to use persistent connections. This is not recommended on shared hosts,
  19.  * and might require additional database/webserver tuning. It can increase
  20.  * performance, however, when the overhead to connect to your database is high
  21.  * (e.g. your database and web server live on different machines).
  22.  */
  23. function db_connect($url) {
  24.   $url = parse_url($url);
  25.  
  26.   $conn_string = ' user='. $url['user'] .' dbname='. substr($url['path'], 1) .' password='. $url['pass'] . ' host=' . $url['host'];
  27.   $conn_string .= isset($url['port']) ? ' port=' . $url['port'] : '';
  28.   $connection = pg_connect($conn_string) or die(pg_last_error());
  29.  
  30.   return $connection;
  31. }
  32.  
  33. /**
  34.  * Helper function for db_query().
  35.  */
  36. function _db_query($query, $debug = 0) {
  37.   global $active_db, $last_result;
  38.   global $queries;
  39.  
  40.   if (variable_get('dev_query', 0)) {
  41.     list($usec, $sec) = explode(' ', microtime());
  42.     $timer = (float)$usec + (float)$sec;
  43.   }
  44.  
  45.   $last_result = pg_query($active_db, $query);
  46.  
  47.   if (variable_get('dev_query', 0)) {
  48.     list($usec, $sec) = explode(' ', microtime());
  49.     $stop = (float)$usec + (float)$sec;
  50.     $diff = $stop - $timer;
  51.     $queries[] = array($query, $diff);
  52.   }
  53.  
  54.   if ($debug) {
  55.     print '<p>query: '. $query .'<br />error:'. pg_last_error() .'</p>';
  56.   }
  57.  
  58.   if ($last_result !== FALSE) {
  59.     return $last_result;
  60.   }
  61.   else {
  62.     trigger_error(pg_last_error() ."\nquery: ". htmlspecialchars($query), E_USER_ERROR);
  63.   }
  64. }
  65.  
  66. /**
  67.  * Fetch one result row from the previous query as an object.
  68.  *
  69.  * @param $result
  70.  *   A database query result resource, as returned from db_query().
  71.  * @return
  72.  *   An object representing the next row of the result. The attributes of this
  73.  *   object are the table fields selected by the query.
  74.  */
  75. function db_fetch_object($result) {
  76.   if ($result) {
  77.     return pg_fetch_object($result);
  78.   }
  79. }
  80.  
  81. /**
  82.  * Fetch one result row from the previous query as an array.
  83.  *
  84.  * @param $result
  85.  *   A database query result resource, as returned from db_query().
  86.  * @return
  87.  *   An associative array representing the next row of the result. The keys of
  88.  *   this object are the names of the table fields selected by the query, and
  89.  *   the values are the field values for this result row.
  90.  */
  91. function db_fetch_array($result) {
  92.   if ($result) {
  93.     return pg_fetch_assoc($result);
  94.   }
  95. }
  96.  
  97. /**
  98.  * Determine how many result rows were found by the preceding query.
  99.  *
  100.  * @param $result
  101.  *   A database query result resource, as returned from db_query().
  102.  * @return
  103.  *   The number of result rows.
  104.  */
  105. function db_num_rows($result) {
  106.   if ($result) {
  107.     return pg_num_rows($result);
  108.   }
  109. }
  110.  
  111. /**
  112.  * Return an individual result field from the previous query.
  113.  *
  114.  * Only use this function if exactly one field is being selected; otherwise,
  115.  * use db_fetch_object() or db_fetch_array().
  116.  *
  117.  * @param $result
  118.  *   A database query result resource, as returned from db_query().
  119.  * @param $row
  120.  *   The index of the row whose result is needed.
  121.  * @return
  122.  *   The resulting field.
  123.  */
  124. function db_result($result, $row = 0) {
  125.   if ($result && pg_num_rows($result) > $row) {
  126.     $res = pg_fetch_row($result, $row);
  127.  
  128.     return $res[0];
  129.   }
  130. }
  131.  
  132. /**
  133.  * Determine whether the previous query caused an error.
  134.  */
  135. function db_error() {
  136.   return pg_last_error();
  137. }
  138.  
  139. /**
  140.  * Return a new unique ID in the given sequence.
  141.  *
  142.  * For compatibility reasons, Drupal does not use auto-numbered fields in its
  143.  * database tables. Instead, this function is used to return a new unique ID
  144.  * of the type requested. If necessary, a new sequence with the given name
  145.  * will be created.
  146.  */
  147. function db_next_id($name) {
  148.   $id = db_result(db_query("SELECT nextval('%s_seq')", db_prefix_tables($name)));
  149.   return $id;
  150. }
  151.  
  152. /**
  153.  * Determine the number of rows changed by the preceding query.
  154.  */
  155. function db_affected_rows() {
  156.   global $last_result;
  157.   return pg_affected_rows($last_result);
  158. }
  159.  
  160. /**
  161.  * Runs a limited-range query in the active database.
  162.  *
  163.  * Use this as a substitute for db_query() when a subset of the query is to be
  164.  * returned.
  165.  * User-supplied arguments to the query should be passed in as separate parameters
  166.  * so that they can be properly escaped to avoid SQL injection attacks.
  167.  *
  168.  * @param $query
  169.  *   A string containing an SQL query.
  170.  * @param ...
  171.  *   A variable number of arguments which are substituted into the query using
  172.  *   printf() syntax. Instead of a variable number of query arguments, you may
  173.  *   also pass a single array containing the query arguments.
  174.  * @param $from
  175.  *   The first result row to return.
  176.  * @param $count
  177.  *   The maximum number of result rows to return.
  178.  * @return
  179.  *   A database query result resource, or FALSE if the query was not executed
  180.  *   correctly.
  181.  */
  182. function db_query_range($query) {
  183.   $args = func_get_args();
  184.   $count = array_pop($args);
  185.   $from = array_pop($args);
  186.  
  187.   $query = db_prefix_tables($query);
  188.   if (count(func_get_args()) > 3) {
  189.     // Check for array (alternative syntax).
  190.     if (is_array($args[1])) {
  191.       $args = array_merge(array($query), $args[1]);
  192.     }
  193.     $args = array_map('db_escape_string', $args);
  194.     $args[0] = $query;
  195.     $query = call_user_func_array('sprintf', $args);
  196.   }
  197.   $query .= ' LIMIT '. $count .' OFFSET '. $from;
  198.   return _db_query($query);
  199. }
  200.  
  201. /**
  202.  * Returns a properly formatted Binary Large OBject value.
  203.  *
  204.  * @param $data
  205.  *   Data to encode.
  206.  * @return
  207.  *  Encoded data.
  208.  */
  209. function db_encode_blob($data) {
  210.   return addcslashes($data, "\0..\37\\");
  211. }
  212.  
  213. /**
  214.  * Returns text from a Binary Large OBject value.
  215.  *
  216.  * @param $data
  217.  *   Data to decode.
  218.  * @return
  219.  *  Decoded data.
  220.  */
  221. function db_decode_blob($data) {
  222.   return stripcslashes($data);
  223. }
  224.  
  225. /**
  226.  * Prepare user input for use in a database query, preventing SQL injection attacks.
  227.  * Note: This function requires PostgreSQL 7.2 or later.
  228.  */
  229. function db_escape_string($text) {
  230.   return pg_escape_string($text);
  231. }
  232.  
  233. /**
  234.  * @} End of "ingroup database".
  235.  */
  236.  
  237. ?>
  238.